#include "BRNNGameScence.h"
#include "Game/GameLib.h"

#include "Main/stdafx.h"
#include "CMD_BROx.h"
#include "BRNNPlayer.h"
#include "Games/NiuNiuGameLogic.h"
#include "GameLib/JniCross/JniFun.h"
#include "Game/Game/MissionWeiXin.h"

#include "Games/GameChatLayer.h"
#include "BRNNRecordLayer.h"
#include "BRNNOnLineLayer.h"
#include "BRNNSoundFun.h"

void BRNNGameScence::initButton()
{
	auto btn = (Button*)_layout->getChildByName("Button_addFriend");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_back");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_dismiss");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_exit");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_online");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_record");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_chat");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_ready");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_talk");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	

	Node* topPanel = (Node*)_layout->getChildByName("Sprite_top");
	m_btnQiangZhuang = (Button*)topPanel->getChildByName("Button_qiangzhuang");
	m_btnQiangZhuang->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
	m_btnXiaZhuang = (Button*)topPanel->getChildByName("Button_xiazhuang");
	m_btnXiaZhuang->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));

	for (int i = 0; i < BTN_MAX; ++i)
	{
		btn = (Button*)_layout->getChildByName(StringUtils::format("Button_opt%d",i + 1));
		btn->addTouchEventListener(CC_CALLBACK_2(BRNNGameScence::BtnCallBack, this));
		m_btns[i] = btn;
	}
}

void BRNNGameScence::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		BRNNSoundFun::playEffect("effect_click");
		if (btn->getName().compare("Button_ready") == 0)
		{
			sendReady();
			defaultState();
		}
		if (btn->getName().compare("Button_addFriend") == 0)
		{
			//MissionWeiXin::Instance().shareTextWeiXin(utility::getScriptString("PrivateTxt2"));
			MissionWeiXin::Instance().shareUrlWeiXin("http://www.rdaohy.com/", utility::getScriptString("PrivateTxt4"), utility::getScriptString("PrivateTxt2"));
		}
		else if (btn->getName().compare("Button_back") == 0 || btn->getName().compare("Button_exit") == 0)
		{
			Button_ExitGameBase();
		}
		else if (btn->getName().compare("Button_dismiss") == 0)
		{
			CMD_GR_Dismiss_Private kNetInfo;
			kNetInfo.bDismiss = 1;
			CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			/*
			auto dismissDialog = CSLoader::createNode("aqmj/commonLayer.csb");
			addChild(dismissDialog, 201, 201);
			dismissDialog->setName("disMissDialog");

			Button* btn = (Button*)dismissDialog->getChildByName("Button_cancel");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);
			});

			btn = (Button*)dismissDialog->getChildByName("Button_confirm");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);

				CMD_GR_Dismiss_Private kNetInfo;
				kNetInfo.bDismiss = 1;
				CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			});
			*/
		}
		else if (btn->getName().compare("Button_help") == 0)
		{
			Layout* helpLayout = (Layout*)_layout->getChildByName("Panel_help");
			helpLayout->setVisible(true);
		}
		else if (btn->getName().compare("Button_chat") == 0)
		{
			GameChatLayer* gChatLayer = GameChatLayer::create();
			addChild(gChatLayer);
		}
		else if (btn->getName().compare("Button_record") == 0)
		{
			BRNNRecordLayer* gChatLayer = BRNNRecordLayer::create();
			addChild(gChatLayer);
			gChatLayer->showList(recordList);

// 			auto recordLayer = CSLoader::createNode("recordLayer.csb");
// 			addChild(recordLayer, BRNN_GAME_RECORD_LAYER, BRNN_GAME_RECORD_LAYER);
// 			recordLayer->setName("recordLayer");
// 
// 			Button* btn = (Button*)recordLayer->getChildByName("Button_close");
// 			btn->addClickEventListener([=](Ref* pSender){
// 				recordLayer->removeFromParentAndCleanup(true);
// 			});
		}
		else if (btn->getName().compare("Button_online") == 0)
		{
			BRNNOnLineLayer* gChatLayer = BRNNOnLineLayer::create();
			addChild(gChatLayer);

// 			auto uListLayer = CSLoader::createNode("uListLayer.csb");
// 			addChild(uListLayer, BRNN_GAME_USERLIST_LAYER, BRNN_GAME_USERLIST_LAYER);
// 			uListLayer->setName("uListLayer");
// 
// 			Button* btn = (Button*)uListLayer->getChildByName("Button_close");
// 			btn->addClickEventListener([=](Ref* pSender){
// 				uListLayer->removeFromParentAndCleanup(true);
// 			});
		}
		else if (btn->getName().compare("Button_opt1") == 0)
		{
			m_betChipValue = 50;
			hiddenAllBtns();
			m_bChipClick = true;
		}
		else if (btn->getName().compare("Button_opt2") == 0)
		{
			m_betChipValue = 100;
			hiddenAllBtns();
			m_bChipClick = true;
		}
		else if (btn->getName().compare("Button_opt3") == 0)
		{
			m_betChipValue = 500;
			hiddenAllBtns();
			m_bChipClick = true;
		}
		else if (btn->getName().compare("Button_opt4") == 0)
		{
			m_betChipValue = 1000;
			hiddenAllBtns();
			m_bChipClick = true;
		}
		else if (btn->getName().compare("Button_talk") == 0)
		{
			Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
			if (sprTalk->isVisible())
			{
				JniFun::stopSoundRecord();
				sprTalk->setVisible(false);
				return;
			}
			sprTalk->setVisible(true);
			JniFun::startSoundRecord();
			scheduleOnce(schedule_selector(BRNNGameScence::endButtonTalk), 5.0f);
			//int iTimeID = TimeManager::Instance().addCerterTimeCB(TIME_CALLBACK(BRNNGameScence::endButtonTalk, this), 5.0f)->iIdex;
			//WidgetFun::setWidgetUserInfo(this, "TalkImagic", "TimeID", utility::toString(iTimeID));
		}
		else if (btn->getName().compare("Button_continue") == 0)
		{
			_layout->getChildByName("Panel_result")->setVisible(false);
			sendReady();
			defaultState();
		}
		else if (btn->getName().compare("Button_share") == 0)
		{
			static BRNNGameScence* pTemp = NULL;
			pTemp = this;
			cocos2d::utils::captureScreen([](bool b, std::string name){
				if (b)
				{
					MissionWeiXin::Instance().shareScreenWeiXin(name);
					pTemp->Button_ExitGameBase();
					pTemp->ExitGame();
					pTemp->_layout->getChildByName("finish_node")->setVisible(false);
				}
			}, "screenshot.png");
		}
	}
		break;
	default:
		break;
	}
}

void BRNNGameScence::endButtonTalk(float dt)
{
	Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
	if (!sprTalk->isVisible())
	{
		return;
	}
	//int iTimeID = utility::parseInt(WidgetFun::getWidgetUserInfo(this, "TalkImagic", "TimeID"));
	//TimeManager::Instance().removeByID(iTimeID);
	sprTalk->setVisible(false);
	std::string kFileName = JniFun::stopSoundRecord();
	sendTalkFile(m_pLocal->GetChairID(), kFileName);
}
void BRNNGameScence::BtnHeadCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		int userIndex = (intptr_t) btn->getUserData();
		BRNNPlayer* pPlayer = m_pPlayer[userIndex];
		MessageBox("show userInfo<===========", "title");
// 		PopScence::Instance().showIPInfo(pPlayer->GetNickName(),
// 			utility::paseInt(pPlayer->GetUserID(), 6),
// 			pPlayer->GetUserInfo()->szLogonIP,
// 			pPlayer->GetUserInfo()->szHeadHttp);
	}
		break;
	default:
		break;
	}
}

void BRNNGameScence::BtnChipsCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	if (!m_bChipClick) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		Layout* chipsNode = (Layout*)pSender;
		if (m_iBankerUser == m_pLocal->GetChairID())
		{
			return;
		}
		else
		{
			int cTimes = (uintptr_t)chipsNode->getUserData();
			cTimes++;
			if (cTimes > 2)
				return;
			chipsNode->setUserData((void*)cTimes);

			CMD_C_PlaceJetton kNetInfo;
			kNetInfo.cbJettonArea = chipsNode->getTag();
			kNetInfo.lJettonScore = m_betChipValue;
			log("------------send kNetInfo chips:%lld", kNetInfo.lJettonScore);
			CServerItem::get()->SendSocketData(MDM_GF_GAME, SUB_C_PLACE_JETTON, &kNetInfo, sizeof(kNetInfo));
		}
	}
		break;
	default:
		break;
	}
}